package com.aruba.drawerapplication;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * 包含SlideLinearLayout用的布局
 */
public class SlideRelativeLayout extends RelativeLayout {

    private SlideLinearLayout slideLinearLayout;
    private SlideBackgroundView slideBackgroundView;
    private boolean isOpen;

    public SlideRelativeLayout(Context context) {
        this(context, null);
    }

    public SlideRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void attachLinearLayout(SlideLinearLayout child, ViewGroup.LayoutParams params) {
        this.slideLinearLayout = child;

        //添加背景view
        slideBackgroundView = new SlideBackgroundView(getContext());
        slideBackgroundView.setBackground(slideLinearLayout.getBackground());
        slideLinearLayout.setBackground(new ColorDrawable(Color.TRANSPARENT));
        addView(slideBackgroundView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        //添加SlideLinearLayout
        addView(child, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    /**
     * 触摸事件分发
     *
     * @param y
     * @param fraction
     */
    public void setTouchY(float y, float fraction) {
        if (isOpen) {
            return;
        }
        if (slideBackgroundView != null) {
            slideBackgroundView.calcBezier(y, fraction);
        }
        if (slideLinearLayout != null) {
            slideLinearLayout.layoutChild(y);
        }
    }

    /**
     * 手指抬起，并分配点击事件
     */
    public void onMotionUp(float x, float y) {
        if (!isOpen || slideLinearLayout == null || !slideLinearLayout.hasItemClickListener())
            return;

        if (slideLinearLayout.onMotionUp(x, y)) {
            
        }
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setItemClickListener(SlideLinearLayout.ItemClickListener itemClickListener) {
        if (slideLinearLayout != null) {
            slideLinearLayout.setItemClickListener(itemClickListener);
        }
    }

}
